package org.example.basic.lambda.stream;


import org.example.basic.entity.UserEntity;
import org.example.basic.entity.UserEntityList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * reduce 案例
 *
 * @author songyafei
 */
public class LambdaReduce {
    public static void main(String[] args) {

        //Integer 集合
        List<Integer> n = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4));
        Integer reduce = n.stream().reduce(0, (a, b) -> a + b);
        System.out.println(reduce);

        //对象类型
        List<UserEntity> u = UserEntityList.getList();


        //场景： 先对集合中某个字段过滤，然后再获取到年龄字段集做sum操作
        int asInt = u.stream().filter(m -> m.getAge() < 20).mapToInt(UserEntity::getAge)
                .reduce((x, y) -> x += y).getAsInt();
        System.out.println(asInt);

        int reduce1 = u.stream().filter(m -> m.getAge() < 30).mapToInt(UserEntity::getAge)
                .reduce(0, (x, y) -> x += y);
        System.out.println(reduce1);



    }

}
